
import React, { useEffect, useState, useRef } from 'react'
import { Link, useHistory } from 'react-router-dom'
export default function NotFount() {
    const [time, setTime] = useState(3)
    const history = useHistory()
    // const ref = useRef(time)
    const timeRef = useRef(-1)
    // useEffect(() => {
    //     timeRef.current = setInterval(() => {
    //         setTime(ref.current - 1)
    //     }, 1000)
    // }, [])
    // useEffect(() => {
    //     ref.current = time
    //     if (time === 0) {
    //         clearInterval(timeRef.current)
    //         history.push('/home')
    //     }
    // }, [time, history])
    useEffect(() => {
        timeRef.current = setInterval(() => {
            setTime(time => {
                // if (time === 1) {
                //     clearInterval(timer)
                //     history.push('/home')
                // }
                return time - 1
            })
        }, 1000)
        return () => clearInterval(timeRef.current)
    }, [])
    useEffect(() => {
        if (time === 0) {
            clearInterval(timeRef.current)
            history.push('/home')
        }
    }, [time, history])
    return (
        <div>
            你访问的页面不存在,{time}秒后跳转到<Link to='/home'>首页</Link>
        </div>
    )
}
